<?php
/**
 * releaseShift Function
 *
 * Release a shift from occupied status to available.
 *
 * @author Manas Kalucha <manaskalucha@gmail.com>
 */
include_once 'UTerrorcode.php';
include_once 'UTcheckAuth.php';
include_once 'UTDBGlobal.php';

/**
 * @param $userName string
 * @param $userPassHash string
 * @param $shiftNotDesired string
 * @return array
 */
function releaseShift($userName, $userPassHash, $shiftNotDesired){
	global $dbaddress;
    global $dbuser;
    global $dbpassword;
    global $dbdatabasename;

	if (checkAuthentication($userName, $userPassHash) == true) {
	
		$conn = mysqli_connect($dbaddress,$dbuser,$dbpassword,$dbdatabasename);
		$shift = json_decode($shiftNotDesired, true);
		$sql = "SELECT * FROM RoleDate";
		$result = $conn->query($sql);
		$updater = "UPDATE RoleDate SET ShiftStatus = 'Vacant' WHERE `roleID` = '$shift[RoleID]' AND `dateID` = '$shift[dateID]'";
		
		if($result->num_rows>0)
		{
			while($row = $result->fetch_assoc())
			{
				if($row['roleID']==$shift['RoleID'] && $row['dateID']==$shift['dateID']){
					$conn->query($updater);
					$error = ErrorCode::successRead;
					$output = array('code' => $error, 'value' => "Update Successful : shift has been released");
				}
			}
		}
		else
		{
			$error = ErrorCode::authFailRead;
			$output = array('code' => $error);
		}
		$conn->close();
		return $output;
	}
}

//TestCode
// $a = array('RoleID' => 2 , 'dateID' =>2);
// $r = releaseShift('user','pass',json_encode($a));
//print_r(json_encode($r));
// print_r($r);
?>
